<template>
  <div class="profit-record-page">

    <div class="content-container">
      <!-- 收益记录部分 -->
      <div class="section">
        <div class="section-header">
          <div class="section-indicator"></div>
          <span class="section-title">{{ t('profitrecord_title') }}</span>
        </div>
        <van-list
          v-model:loading="loading"
          :finished="finished"
          :finished-text="t('profitrecord_noMoreData')"
          @load="loadProfitRecords"
        >
          <div class="record-list">
            <div class="record-item" v-for="(item, index) in profitRecords" :key="index">
              <div class="record-info">
                <div class="record-amount">COP{{ formatNumber(item.number) }}</div>
                <div class="record-desc">{{ t('profitrecord_reward') }}</div>
              </div>
              <div class="record-time">{{ item.writedate }}</div>
            </div>
          </div>
        </van-list>
      </div>

      <!-- 摘要部分 -->
      <div class="section">
        <div class="section-header">
          <div class="section-indicator"></div>
          <span class="section-title">{{ t('profitrecord_summary') }}</span>
        </div>

        <div class="summary-list">
          <div class="summary-item">
            <div class="summary-label">{{ t('profitrecord_total') }}</div>
            <div class="summary-value">COP{{ formatNumber(income) }}</div>
          </div>
          <div class="summary-item">
            <div class="summary-label">{{ t('profitrecord_investTime') }}</div>
            <div class="summary-value">{{ endTime }}</div>
          </div>
          <div class="summary-item">
            <div class="summary-label">{{ t('profitrecord_validPeriod') }}</div>
            <div class="summary-value">{{ creatTime }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { formatNumber } from '@/utils/public';
import { lockMallOrderIncome } from '@/api/products';

const router = useRouter();
const route = useRoute();

const profitRecords = ref<any[]>([]);
const income = ref((Array.isArray(route.query.income) ? route.query.income[0] : route.query.income) || '0');
const endTime = ref(route.query.endTime || '');
const creatTime = ref(route.query.creatTime || '');
const loading = ref(false);
const page = ref(1);
const pageSize = 20;
const finished = ref(false);
const {t} = useI18n()
// 加载收益明细
async function loadProfitRecords() {
  if (finished.value) return;
  loading.value = true;
  try {
    const res = await lockMallOrderIncome({
      id: route.query.id as string,
      page: page.value.toString(),
      limit: pageSize.toString()
    });
    if (res.code === 200) {
      const list = res.data || [];
      if (list.length < pageSize) finished.value = true;
      profitRecords.value = [...profitRecords.value, ...list];
      page.value++;
    } else {
      finished.value = true;
    }
  } catch (e) {
    finished.value = true;
  } finally {
    loading.value = false;
  }
}

onMounted(() => {
  loadProfitRecords();
});

</script>

<style scoped>
.profit-record-page {
  min-height: 100vh;
  background: #1B1410;
  color: #F6EADE;
  position: relative;
}

.content-container {
  padding: 15px;
}

.section {
  margin-bottom: 20px;
  background: rgba(38, 32, 27, 0.8);
  border-radius: 8px;
  overflow: hidden;
}

.section-header {
  display: flex;
  align-items: center;
  padding: 12px 15px;
}

.section-indicator {
  width: 3px;
  height: 15px;
  background: #F6EAD8;
  margin-right: 8px;
  border-radius: 1.5px;
}

.section-title {
  font-size: 15px;
  font-weight: 500;
  color: #F6EADE;
}

.record-list {
  padding: 0 15px;
}

.record-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid rgba(84, 76, 70, 0.5);
}

.record-item:last-child {
  border-bottom: none;
}

.record-info {
  display: flex;
  flex-direction: column;
}

.record-amount {
  font-size: 15px;
  font-weight: 500;
  color: #F6EADE;
  margin-bottom: 4px;
}

.record-desc {
  font-size: 13px;
  color: rgba(246, 234, 222, 0.6);
}

.record-time {
  font-size: 12px;
  color: rgba(246, 234, 222, 0.5);
  align-self: flex-start;
}

.summary-list {
  padding: 0 15px 15px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid rgba(84, 76, 70, 0.5);
}

.summary-item:last-child {
  border-bottom: none;
}

.summary-label {
  font-size: 14px;
  color: rgba(246, 234, 222, 0.6);
}

.summary-value {
  font-size: 14px;
  color: #F6EADE;
  font-weight: 500;
}
</style>
